package com.borrow.jiemihua.mainUI.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseActivity;
import com.borrow.jiemihua.base.BaseConstant;
import com.borrow.jiemihua.base.URLConstant;
import com.borrow.jiemihua.bean.MessageMyBean;
import com.borrow.jiemihua.utils.SharedPreferencesUtils;
import com.borrow.jiemihua.utils.xutils3.MyCallBack;
import com.borrow.jiemihua.utils.xutils3.XUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cc.duduhuo.custoast.CusToast;

@ContentView(R.layout.activity_feedback)
public class FeedbackActivity extends BaseActivity {
    @ViewInject(R.id.tv_top_title)
    private TextView tv_top_title;

    @ViewInject(R.id.et_content)
    private EditText et_content;
    @ViewInject(R.id.et_email)
    private EditText et_email;
    @ViewInject(R.id.tv_shjd)
    private TextView tv_shjd;
    @ViewInject(R.id.tv_lcfs)
    private TextView tv_lcfs;
    @ViewInject(R.id.tv_cpsx)
    private TextView tv_cpsx;
    @ViewInject(R.id.tv_syty)
    private TextView tv_syty;
    @ViewInject(R.id.tv_appbwd)
    private TextView tv_appbwd;
    @ViewInject(R.id.tv_qt)
    private TextView tv_qt;

    private int zt=0;
    private String fklx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initData();
        initView();
    }

    @Override
    protected void initData() {
        tv_top_title.setText("意见反馈");

    }

    @Override
    protected void initView() {

    }
    private String content,emain;
    @Event(value = {R.id.imgbtn_left, R.id.tv_tj,R.id.tv_shjd,R.id.tv_lcfs, R.id.tv_cpsx, R.id.tv_syty, R.id.tv_appbwd, R.id.tv_qt}, type = View.OnClickListener.class)
    private void getEvent(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.imgbtn_left:
                finish();
                break;
            case R.id.tv_tj:
                if(zt==1){
                    fklx=tv_shjd.getText().toString();
                }else if(zt==2){
                    fklx=tv_lcfs.getText().toString();
                }else if(zt==3){
                    fklx=tv_cpsx.getText().toString();
                }else if(zt==4){
                    fklx=tv_syty.getText().toString();
                }else if(zt==5){
                    fklx=tv_appbwd.getText().toString();
                }else if(zt==6){
                    fklx=tv_qt.getText().toString();
                }
                uptijiao();
                break;
            case R.id.tv_shjd:
                zt=1;
                tv_shjd.setBackgroundResource(R.drawable.feed_bg_blue);
                tv_shjd.setTextColor(getResources().getColor(R.color.white));
                tv_lcfs.setBackgroundResource(R.drawable.feed_bg);
                tv_lcfs.setTextColor(getResources().getColor(R.color.text_6));
                tv_cpsx.setBackgroundResource(R.drawable.feed_bg);
                tv_cpsx.setTextColor(getResources().getColor(R.color.text_6));
                tv_syty.setBackgroundResource(R.drawable.feed_bg);
                tv_syty.setTextColor(getResources().getColor(R.color.text_6));
                tv_appbwd.setBackgroundResource(R.drawable.feed_bg);
                tv_appbwd.setTextColor(getResources().getColor(R.color.text_6));
                tv_qt.setBackgroundResource(R.drawable.feed_bg);
                tv_qt.setTextColor(getResources().getColor(R.color.text_6));
                break;
            case R.id.tv_lcfs:
                zt=2;
                tv_shjd.setBackgroundResource(R.drawable.feed_bg);
                tv_shjd.setTextColor(getResources().getColor(R.color.text_6));
                tv_lcfs.setBackgroundResource(R.drawable.feed_bg_blue);
                tv_lcfs.setTextColor(getResources().getColor(R.color.white));
                tv_cpsx.setBackgroundResource(R.drawable.feed_bg);
                tv_cpsx.setTextColor(getResources().getColor(R.color.text_6));
                tv_syty.setBackgroundResource(R.drawable.feed_bg);
                tv_syty.setTextColor(getResources().getColor(R.color.text_6));
                tv_appbwd.setBackgroundResource(R.drawable.feed_bg);
                tv_appbwd.setTextColor(getResources().getColor(R.color.text_6));
                tv_qt.setBackgroundResource(R.drawable.feed_bg);
                tv_qt.setTextColor(getResources().getColor(R.color.text_6));
                break;
            case R.id.tv_cpsx:
                zt=3;
                tv_shjd.setBackgroundResource(R.drawable.feed_bg);
                tv_shjd.setTextColor(getResources().getColor(R.color.text_6));
                tv_cpsx.setBackgroundResource(R.drawable.feed_bg_blue);
                tv_cpsx.setTextColor(getResources().getColor(R.color.white));
                tv_lcfs.setBackgroundResource(R.drawable.feed_bg);
                tv_lcfs.setTextColor(getResources().getColor(R.color.text_6));
                tv_syty.setBackgroundResource(R.drawable.feed_bg);
                tv_syty.setTextColor(getResources().getColor(R.color.text_6));
                tv_appbwd.setBackgroundResource(R.drawable.feed_bg);
                tv_appbwd.setTextColor(getResources().getColor(R.color.text_6));
                tv_qt.setBackgroundResource(R.drawable.feed_bg);
                tv_qt.setTextColor(getResources().getColor(R.color.text_6));
                break;
            case R.id.tv_syty:
                zt=4;
                tv_shjd.setBackgroundResource(R.drawable.feed_bg);
                tv_shjd.setTextColor(getResources().getColor(R.color.text_6));
                tv_lcfs.setBackgroundResource(R.drawable.feed_bg);
                tv_lcfs.setTextColor(getResources().getColor(R.color.text_6));
                tv_cpsx.setBackgroundResource(R.drawable.feed_bg);
                tv_cpsx.setTextColor(getResources().getColor(R.color.text_6));
                tv_syty.setBackgroundResource(R.drawable.feed_bg_blue);
                tv_syty.setTextColor(getResources().getColor(R.color.white));
                tv_appbwd.setBackgroundResource(R.drawable.feed_bg);
                tv_appbwd.setTextColor(getResources().getColor(R.color.text_6));
                tv_qt.setBackgroundResource(R.drawable.feed_bg);
                tv_qt.setTextColor(getResources().getColor(R.color.text_6));
                break;
            case R.id.tv_appbwd:
                zt=5;
                tv_shjd.setBackgroundResource(R.drawable.feed_bg);
                tv_shjd.setTextColor(getResources().getColor(R.color.text_6));
                tv_lcfs.setBackgroundResource(R.drawable.feed_bg);
                tv_lcfs.setTextColor(getResources().getColor(R.color.text_6));
                tv_cpsx.setBackgroundResource(R.drawable.feed_bg);
                tv_cpsx.setTextColor(getResources().getColor(R.color.text_6));
                tv_syty.setBackgroundResource(R.drawable.feed_bg);
                tv_syty.setTextColor(getResources().getColor(R.color.text_6));
                tv_appbwd.setBackgroundResource(R.drawable.feed_bg_blue);
                tv_appbwd.setTextColor(getResources().getColor(R.color.white));
                tv_qt.setBackgroundResource(R.drawable.feed_bg);
                tv_qt.setTextColor(getResources().getColor(R.color.text_6));
                break;
            case R.id.tv_qt:
                zt=6;
                tv_shjd.setBackgroundResource(R.drawable.feed_bg);
                tv_shjd.setTextColor(getResources().getColor(R.color.text_6));
                tv_lcfs.setBackgroundResource(R.drawable.feed_bg);
                tv_lcfs.setTextColor(getResources().getColor(R.color.text_6));
                tv_cpsx.setBackgroundResource(R.drawable.feed_bg);
                tv_cpsx.setTextColor(getResources().getColor(R.color.text_6));
                tv_syty.setBackgroundResource(R.drawable.feed_bg);
                tv_syty.setTextColor(getResources().getColor(R.color.text_6));
                tv_appbwd.setBackgroundResource(R.drawable.feed_bg);
                tv_appbwd.setTextColor(getResources().getColor(R.color.text_6));
                tv_qt.setBackgroundResource(R.drawable.feed_bg_blue);
                tv_qt.setTextColor(getResources().getColor(R.color.white));
                break;
            default:
                break;
        }
    }
    private void uptijiao(){
        content=et_content.getText().toString().trim();
        emain=et_email.getText().toString().trim();
        if(zt>0&&emain.contains("@")&&!content.equals("")){
            upfeedData();
        }else if(zt==0){
            CusToast.showToast("请选择反馈主题");
            return;
        }else if(content.equals("")||content==null){
            CusToast.showToast("请详细描述您的问题或意见");
            return;
        }else if(!emain.contains("@")){
            CusToast.showToast("请输入正确的邮箱");
            return;
        }

    }
    private void upfeedData() {
        Map<String,Object> map=new HashMap<>();
        String userid = SharedPreferencesUtils.getString(this, BaseConstant.SPConstant.USERID, "");
        map.put("userId",userid);
        map.put("theme",fklx);
        map.put("detail",content);
        map.put("email",emain);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("--提交反馈参数--" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(URLConstant.MY_FEEDBACK,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==意见反馈",result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("isSuccess");
                    if(res.equals("1")){
                        CusToast.showToast("您的反馈已提交成功，我们将及时跟进解决");
                        finish();
                    }else{
                        CusToast.showToast(jsonObject.optString("msg"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
                closeDialog();
            }
        });
    }

}
